//
//  Authentication.swift
//  Fancyswap
//
//  Created by Karim Safraoui on 2015-03-14.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import Foundation

var fancy_token             = ""
var fancy_user_username     = ""
var fancy_user_zip          = ""
var fancy_user_locale       = ""
var fancy_user_email        = ""
var fancy_user_city         = ""
var fancy_un = ""
var fancy_pw = ""

let fancy_keychain_un       = "Fancyswap.un"
let fancy_keychain_pw       = "Fancyswap.pw"
let fancy_keychain_token            = "Fancyswap.token"
let fancy_ketchain_user_username    = "Fancyswap.user.username"
let fancy_ketchain_user_zip         = "Fancyswap.user.zip"
let fancy_ketchain_user_locale      = "Fancyswap.user.locale"
let fancy_ketchain_user_email       = "Fancyswap.user.email"
let fancy_ketchain_user_city        = "Fancyswap.user.city"


class Authentification {
    
    let t = FancyTools()
    
    func isLoggedIn() -> Bool {
        
        if Authentification.getToken().isEmpty {
            return false
        }
        return true
    }
    
    class func IsDone() -> Bool {
        return !(getToken().isEmpty)
    }
    class func getToken() -> String!{
        
        if let rep = TegKeychain.get(fancy_keychain_token){
            fancy_token = rep
            if let un = TegKeychain.get(fancy_keychain_un){
                fancy_un = un
            }
            if let pw = TegKeychain.get(fancy_keychain_pw){
                fancy_pw = pw
            }

            return rep
        }
        return fancy_token
    }
    
    func authenticate( json_rep: JSON, remember: Bool, un:String, pw:String ) -> Bool {
    
        let status  = json_rep["status"].int
        
        if status == 0 {
            
            let message = json_rep["message"].string!
            t.alert_push("Attention", msg: message)
            return false
        }
        else {
            
            let fancy_user  = json_rep["user"] // .dictionary!
            let token       = fancy_user["token"].stringValue
            CurrUser = FancyUser(swiftyDic:fancy_user)
            
            if( !token.isEmpty ){
                
                fancy_token            = token
                fancy_user_username    = fancy_user["username"].stringValue
                fancy_user_zip         = fancy_user["zip"].stringValue
                fancy_user_locale      = fancy_user["locale"].stringValue
                fancy_user_email       = fancy_user["email"].stringValue
                fancy_user_city        = fancy_user["city"].stringValue
                fancy_un = un
                fancy_pw = pw
                
                if(remember){
                    TegKeychain.set(fancy_keychain_token,value:token)
                    TegKeychain.set(fancy_keychain_un,value:un)
                    TegKeychain.set(fancy_keychain_pw,value:pw)
                    TegKeychain.set(fancy_ketchain_user_username,value:fancy_user_username)
                    TegKeychain.set(fancy_ketchain_user_zip,value:fancy_user_zip)
                    TegKeychain.set(fancy_ketchain_user_locale,value:fancy_user_locale)
                    TegKeychain.set(fancy_ketchain_user_email,value:fancy_user_email)
                    TegKeychain.set(fancy_ketchain_user_city,value:fancy_user_city)
                }
                
                return true
            }
            else {
                t.alert_push("Attention", msg: "Bug !!!! Token non fourni")
                return false
            }
        }
    }
    
    func signout(){
            
        // vider le token
        TegKeychain.delete(fancy_keychain_token)
        TegKeychain.delete(fancy_keychain_un)
        TegKeychain.delete(fancy_keychain_pw)
        TegKeychain.delete(fancy_ketchain_user_username)
        TegKeychain.delete(fancy_ketchain_user_zip)
        TegKeychain.delete(fancy_ketchain_user_locale)
        TegKeychain.delete(fancy_ketchain_user_email)
        TegKeychain.delete(fancy_ketchain_user_city)
        
        fancy_token             = ""
        fancy_un = ""
        fancy_pw = ""
        fancy_user_username     = ""
        fancy_user_zip          = ""
        fancy_user_locale       = ""
        fancy_user_email        = ""
        fancy_user_city         = ""
    }
}
